/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sgs.persist;

import br.com.sgs.basic.GenericDAO;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 *
 * @author MaUrO FiLhO
 */
@Entity
@Table(name = "area")
@javax.persistence.SequenceGenerator(name = "area_id_area_seq", sequenceName = "area_id_area_seq")
@NamedQueries({
    @NamedQuery(name = "Area.findAll", query = "SELECT a FROM Area a"),
    @NamedQuery(name = "Area.findByIdArea", query = "SELECT a FROM Area a WHERE a.idArea = :idArea"),
    @NamedQuery(name = "Area.findByNome", query = "SELECT a FROM Area a WHERE a.nome = :nome"),
    @NamedQuery(name = "Area.findByNumero", query = "SELECT a FROM Area a WHERE a.numero = :numero")})
public class Area implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "area_id_area_seq")
    @Basic(optional = false)
    @Column(name = "id_area")
    private Integer idArea;
    @Basic(optional = false)
    @Column(name = "nome")
    private String nome;
    @Column(name = "numero")
    private String numero;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "area")
    private List<Requisicao> requisicaoList;
    @JoinColumn(name = "id_obra", referencedColumnName = "id_obra")
    @ManyToOne(optional = false)
    private Obra obra;
    @OneToMany(mappedBy = "area")
    private List<Pedido> pedidoList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "area")
    private List<Equipamento> equipamentoList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "area")
    private List<Material> materialList;

    public Area() {
    }

    public Area(Integer idArea) {
        this.idArea = idArea;
    }

    public Area(Integer idArea, String nome) {
        this.idArea = idArea;
        this.nome = nome;
    }

    public Integer getIdArea() {
        return idArea;
    }

    public void setIdArea(Integer idArea) {
        this.idArea = idArea;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }

    public List<Requisicao> getRequisicaoList() {
        return requisicaoList;
    }

    public void setRequisicaoList(List<Requisicao> requisicaoList) {
        this.requisicaoList = requisicaoList;
    }

    public Obra getObra() {
        return obra;
    }

    public void setObra(Obra obra) {
        this.obra = obra;
    }

    public List<Pedido> getPedidoList() {
        return pedidoList;
    }

    public void setPedidoList(List<Pedido> pedidoList) {
        this.pedidoList = pedidoList;
    }

    public List<Equipamento> getEquipamentoList() {
        return equipamentoList;
    }

    public void setEquipamentoList(List<Equipamento> equipamentoList) {
        this.equipamentoList = equipamentoList;
    }

    public List<Material> getMaterialList() {
        return materialList;
    }

    public void setMaterialList(List<Material> materialList) {
        this.materialList = materialList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idArea != null ? idArea.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Area)) {
            return false;
        }
        Area other = (Area) object;
        if ((this.idArea == null && other.idArea != null) || (this.idArea != null && !this.idArea.equals(other.idArea))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "br.com.sgs.persist.Area[idArea=" + idArea + "]";
    }
    public static final String FALHA = "falha";
    public static final String SUCESSO = "sucesso";
    public static final String CANCELAR = "cancelar";

    public String alterarArea(Area area) {
        GenericDAO DAO = new GenericDAO();
        String retorno = FALHA;
        try {
            DAO.alterar(area);
            retorno = SUCESSO;
        } catch (Exception e) {
            System.out.print(e);
            retorno = FALHA;
        }
        return retorno;
    }

    public String excluirArea(Area area) {
        GenericDAO DAO = new GenericDAO();
        String retorno = FALHA;
        try {
            DAO.deletar(area);
            retorno = SUCESSO;
        } catch (Exception e) {
            System.out.print(e);
            retorno = FALHA;
        }
        return retorno;
    }

    public String cancelarArea() {
        return CANCELAR;
    }

    public String cadastrarArea(Area area) {
        GenericDAO DAO = new GenericDAO();
        String retorno = FALHA;
        try {

            DAO.inserir(area);
            retorno = SUCESSO;

        } catch (Exception e) {
            System.out.print("Falha no Bean Cadarea: " + e);
            retorno = FALHA;
        }

        return retorno;
    }
 }
